package com.mall.manager.userlogin.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.mall.untils.MD5Util;


@Service("LoginService")  
public class ManagerUserLoginService {  
	private Logger logger = LoggerFactory.getLogger(ManagerUserLoginService.class);
    @Resource
    private JdbcTemplate jdbcTemplate;
    

	public Map<String, Object> manager_user_login(Map<String, Object> paraMap){  
	
		String username = MapUtils.getString(paraMap, "username");
		String password = MD5Util.convertMD5(MapUtils.getString(paraMap, "password"));
				
		try { 
		String sql="select * from t_user where user_name = ? and pwd = ?";    
						
		Map<String, Object> map = jdbcTemplate.queryForMap(sql,username,password);
			
			return map;
						
		} catch (Exception e) { 
			logger.info("用户验证失败！");
			return null;
		}
		
	
	}
	
	
	/**根据用户Id获取权限level
	 * @param paraMap
	 * @return
	 */
	public 	List<Map<String, Object>> getPowerbyId(int currentuserId,int level) {
	    	try {
	    		String sql = "";
	    		if(currentuserId== 0){
		   	    	  return null;
		   	   	}else{  	    
		   	   		sql = "select distinct p.id,p.parent_id,p.name,p.poweraddr,p.level,p.remarks from t_power p join t_power_part pp on pp.power_id=p.id join t_part pt on pp.part_id=pt.id join t_user_part up on up.part_id=pt.id join t_user tu on tu.id=up.user_id  where tu.id=? and p.level=?";
		   	   	}
	 	       	List<Map<String, Object>> queryForList = jdbcTemplate.queryForList(sql,currentuserId,level);
	 	        return queryForList;
	 	    } catch (Exception e) {
	 	        this.logger.info("用户查询权限失败！");
	 	        return null;
	 	    }
		}

	/**根据用户Id获取所有权限
	 * @param paraMap
	 * @return
	 */
	public 	List<Map<String, Object>> getAllpower(int currentuserId) {
	    	try {
	    		String sql = "";
	    		if(currentuserId== 0){
		   	    	  return null;
		   	   	}else{  	    
		   	   		sql = "select p.id,p.parent_id,p.name,p.poweraddr,p.level,p.remarks from t_power p join t_power_part pp on pp.power_id=p.id join t_part pt on pp.part_id=pt.id join t_user_part up on up.part_id=pt.id join t_user tu on tu.id=up.user_id  where tu.id=?";
		   	   	}
	 	       	List<Map<String, Object>> queryForList = jdbcTemplate.queryForList(sql,currentuserId);
	 	        return queryForList;
	 	    } catch (Exception e) {
	 	        this.logger.info("用户查询权限失败！");
	 	        return null;
	 	    }
		}
	
}
